{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit PersistenceHelper;

interface

uses Classes;

type
  TAHMPersistenceHelper = class
  public
    class function LoadObject(Instance: TPersistent; Filename: String): Boolean;
    class function SaveObject(Instance: TPersistent; Filename: String): Boolean;
  end;


implementation

uses Windows, SysUtils, JvgXMLSerializer;

// TAHMPersistenceHelper

class function TAHMPersistenceHelper.LoadObject(Instance: TPersistent; Filename: String): Boolean;
var
  FileStream: TFileStream;
  Serializer: TJvgXMLSerializer;
begin
  // Set default result - failed to load
  Result := False;

  // Check we were passed a valid instance
  if not Assigned(Instance) then Exit;

  try
    FileStream := TFileStream.Create(Filename, fmOpenRead or fmShareDenyNone);
    try
      Serializer := TJvgXMLSerializer.Create(nil);
      try
        // Ignore blank values to reduce file size a bit & encode reserved chars
        Serializer.ReplaceReservedSymbols := True;

        // Now load XML format from file and convert to binary representation
        Serializer.DeSerialize(Instance, FileStream);
        Result := True; // Loaded :)
      finally
        Serializer.Free;
      end;
    finally
      FileStream.Free;
    end;
  except
    // Catch any exceptions silently & return false
  end;
end;

class function TAHMPersistenceHelper.SaveObject(Instance: TPersistent; Filename: String): Boolean;
var
  FileStream: TFileStream;
  Serializer: TJvgXMLSerializer;
begin
  // Set default result - failed to save
  Result := False;

  // Check we were passed a valid instance
  if not Assigned(Instance) then Exit;

  // Delete existing file to be sure it's empty
  if FileExists(Filename) then
    if not Windows.DeleteFile(PChar(FileName)) then Exit;

  try
    FileStream := TFileStream.Create(Filename, fmCreate or fmShareDenyWrite);
    try
      Serializer := TJvgXMLSerializer.Create(nil);
      try
        // Ignore blank values to reduce file size a bit & encode reserved chars
        Serializer.ExcludeEmptyValues := True;
        Serializer.ReplaceReservedSymbols := True;

        // Now convert binary representation to XML format & write to file
        Serializer.Serialize(Instance, FileStream);
        Result := True; // Saved :)
      finally
        Serializer.Free;
      end;
    finally
      FileStream.Free;
    end;
  except
    // Catch any exceptions silently & return false
  end;
end;


end.
